<?php

/*
 * Copyright (c) Madbit 2012. All Rights Reserved.
 * Reproduction in whole or in part is prohibited without the
 * written consent of the copyright owner.
 */

/**
 * Description of ActionDispatcher
 *
 * @author Alessio Fiore
 */
include_once '../utils/includer.php';
includeFile("protected/controllers/LoginController.php");
includeFileOnce("utils/redirect.php");
includeFileOnce("utils/directories.php");

if (!empty($_GET['action'])) {
    $action = $_GET['action'];

    if ($action == "login") {
        $username = $_POST['username'];
        $password = $_POST['password'];
        $challenge = $_POST['challenge'];
        $setcookie = $_POST['setcookie'];

        $loginController = new LoginController();

        $res = $loginController->login($username, $password, $challenge, $setcookie);
        if ($res) {

            $host = "http://" . getHostBaseDir() . "/administrator/index.php?idc=1";
            redirect($host, FALSE);
        } else {
            includeFileOnce("protected/messages/it/messages.php");
            $messages = getBundleMessages();
            echo $messages['login.wrong_data'];
        }
    } else if ($action == "logout") {
        $loginController = new LoginController();
        $res = $loginController->logout();

        $host = "http://" . getHostBaseDir();
        redirect($host, FALSE);
    }
}
else
    echo "No action selected";
?>
